package com.jsb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.service.QuestionService;
import com.jsb.service.RedEnvelopesService;
import com.jsb.service.SecretlyService;
import com.jsb.service.UserService;
import com.jsb.model.Question;
import com.jsb.model.QuestionExample;
import com.jsb.model.QuestionExample.Criteria;
import com.jsb.model.RedEnvelopes;
import com.jsb.model.RedEnvelopesExample;
import com.jsb.model.Secretly;
import com.jsb.model.SecretlyExample;
import com.jsb.model.User;
import com.jsb.model.UserExample;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.UserQuestion;
import com.jsb.dto.UserRedEnvelopes;
import com.jsb.dto.UserSecretly;
/**
 * 用于会员卡支出流水的controller
 * @author chengwl
 *
 */
@Controller
public class CardOutController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/cardOut/list.do";
	
	@Resource
	private QuestionService questionService;
	@Resource
	private UserService userService;
	@Resource
	private SecretlyService secretlyService;
	@Resource
	private RedEnvelopesService redEnvelopesService;
	
	@RequestMapping("/cardOutQuestion/list")
	public String list(
			Model model,String phone,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		QuestionExample questionExample = new QuestionExample();
		Criteria criteria = questionExample.createCriteria();
		if(StringUtils.isNotBlank(phone)){
			criteria.andPhoneNumLike("%"+phone+"%");
		}
		//封装条件
		criteria.andPayStatusEqualTo("0");
		criteria.andPayTypeEqualTo("1");
		int totalCount = questionService.countQuestion(questionExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		questionExample.setPageIndex((pageNum - 1) * pageSize);
		questionExample.setPageSize(pageSize);
		//排序
		questionExample.setOrderByClause("date desc");
		List<Question> questions = questionService.getPageQuestion(questionExample);
		List<UserQuestion> userQuestions = new ArrayList<>();
		//将invoice对象封装进purchaseorders中
		for (Question question : questions) {
			//创建大对象
			UserQuestion userQuestion= new UserQuestion();
			//将小对象封装到大对象中
			userQuestion.setQuestion(question);
			
			String phoneNum1 = question.getPhoneNum();
			UserExample userExample= new UserExample();
			userExample.createCriteria().andPhoneNumberEqualTo(phoneNum1);
			//根据用户电话号码，查询出对象，结果也只能有一条
			List<User> users = userService.selectByExample(userExample);
			if(users !=null && users.size()>0){
				//将小对象封装到大对象中
				userQuestion.setUser(users.get(0));
			}
			//将大对象放到集合中
			userQuestions.add(userQuestion);
		}
		//将条件放到model中
		model.addAttribute("phone", phone);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		//将数据放到model中
		model.addAttribute("userQuestions", userQuestions);
		return "CardOutQuestion/list";
	}
	@RequestMapping("/cardOutRedEnvelopes/list")
	public String cardOutRedEnvelopesList(
			Model model,String phone,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		RedEnvelopesExample redEnvelopesExample = new RedEnvelopesExample();
		com.jsb.model.RedEnvelopesExample.Criteria createCriteria = redEnvelopesExample.createCriteria();
		if(StringUtils.isNotBlank(phone)){
			createCriteria.andSendPhoneNumLike("%"+phone+"%");
		}
		//封装条件
//		createCriteria.andPayStatusEqualTo("0");
		createCriteria.andPayTypeEqualTo("1");
		int totalCount = redEnvelopesService.countRedEnvelopes(redEnvelopesExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		redEnvelopesExample.setPageIndex((pageNum - 1) * pageSize);
		redEnvelopesExample.setPageSize(pageSize);
		//排序
		redEnvelopesExample.setOrderByClause("sendDate desc");
		List<RedEnvelopes> redEnvelopess = redEnvelopesService.getPageRedEnvelopes(redEnvelopesExample);
		List<UserRedEnvelopes> userRedEnvelopess = new ArrayList<>();
		//将invoice对象封装进purchaseorders中
		for (RedEnvelopes redEnvelopes : redEnvelopess) {
			//创建大对象
			UserRedEnvelopes userRedEnvelopes= new UserRedEnvelopes();
			//将小对象封装到大对象中
			userRedEnvelopes.setRedEnvelopes(redEnvelopes);
			
			String phoneNum1 = redEnvelopes.getSendPhoneNum();
			UserExample userExample= new UserExample();
			userExample.createCriteria().andPhoneNumberEqualTo(phoneNum1);
			//根据用户电话号码，查询出对象，结果也只能有一条
			List<User> users = userService.selectByExample(userExample);
			if(users !=null && users.size()>0){
				//将小对象封装到大对象中
				userRedEnvelopes.setUser(users.get(0));
			}
			//将大对象放到集合中
			userRedEnvelopess.add(userRedEnvelopes);
		}
		//将条件放到model中
		model.addAttribute("phone", phone);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		//将数据放到model中
		model.addAttribute("userRedEnvelopess", userRedEnvelopess);
		return "CardOutRedEnvelopes/list";
	}
	@RequestMapping("/cardOutSecretly/list")
	public String cardOutSecretlyList(
			Model model,String phone,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		SecretlyExample secretlyExample = new SecretlyExample();
		com.jsb.model.SecretlyExample.Criteria createCriteria = secretlyExample.createCriteria();
		if(StringUtils.isNotBlank(phone)){
			createCriteria.andPeepPhoneNumLike("%"+phone+"%");
		}
		//封装条件
		createCriteria.andPayStatusEqualTo("0");
		createCriteria.andPayTypeEqualTo("1");
		int totalCount = secretlyService.countSecretly(secretlyExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		secretlyExample.setPageIndex((pageNum - 1) * pageSize);
		secretlyExample.setPageSize(pageSize);
		//排序
		secretlyExample.setOrderByClause("secretlyDate desc");
		List<Secretly> secretlies = secretlyService.getPageSecretly(secretlyExample);
		List<UserSecretly>  userSecretlies= new ArrayList<>();
		//将invoice对象封装进purchaseorders中
		for (Secretly secretly : secretlies) {
			//创建大对象
			UserSecretly userSecretly= new UserSecretly();
			//将小对象封装到大对象中
			userSecretly.setSecretly(secretly);
			
			String phoneNum1 = secretly.getPeepPhoneNum();
			UserExample userExample= new UserExample();
			userExample.createCriteria().andPhoneNumberEqualTo(phoneNum1);
			//根据用户电话号码，查询出对象，结果也只能有一条
			List<User> users = userService.selectByExample(userExample);
			if(users !=null && users.size()>0){
				//将小对象封装到大对象中
				userSecretly.setUser(users.get(0));
			}
			//将大对象放到集合中
			userSecretlies.add(userSecretly);
		}
		//将条件放到model中
		model.addAttribute("phone", phone);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		//将数据放到model中
		model.addAttribute("userSecretlies", userSecretlies);
		return "CardOutSecretly/list";
	}
	
}
